package com.example.controller;

import com.example.dto.Oauth2RegisteredClientDTO;
import com.example.dto.Oauth2RegisteredClientQueryDTO;
import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.service.Oauth2RegisteredClientService;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * OAuth2客户端应用
 */
@RestController
@RequestMapping("/oauth2/client")
@Tag(name = "OAuth2客户端应用管理")
public class Oauth2RegisteredClientController {
    @Autowired
    private Oauth2RegisteredClientService oauth2RegisteredClientService;

    @PostMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('sys:sysclient:page')")
    public Result<PageData<Oauth2RegisteredClientDTO>> page(@RequestBody Oauth2RegisteredClientQueryDTO params) {
        PageData<Oauth2RegisteredClientDTO> page = oauth2RegisteredClientService.selectPage(params);
        return new Result<PageData<Oauth2RegisteredClientDTO>>().ok(page);
    }

    @PostMapping("list")
    @Operation(summary = "列表")
    @PreAuthorize("hasAuthority('sys:sysclient:list')")
    public Result<List<Oauth2RegisteredClientDTO>> list(@RequestBody Oauth2RegisteredClientQueryDTO params) {
        List<Oauth2RegisteredClientDTO> list = oauth2RegisteredClientService.list(params);
        return new Result<List<Oauth2RegisteredClientDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:sysclient:info')")
    public Result<Oauth2RegisteredClientDTO> get(@PathVariable("id") String id) {
        Oauth2RegisteredClientDTO data = oauth2RegisteredClientService.get(id);
        return new Result<Oauth2RegisteredClientDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('sys:sysclient:save')")
    public Result save(@RequestBody Oauth2RegisteredClientDTO dto) {
        ValidatorUtils.validateEntity(dto, DefaultGroup.class, AddGroup.class);


        oauth2RegisteredClientService.save(dto);
        return Result.ok();
    }




    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('sys:sysclient:update')")
    public Result update(@RequestBody Oauth2RegisteredClientDTO dto) {
        ValidatorUtils.validateEntity(dto, DefaultGroup.class, UpdateGroup.class);
        oauth2RegisteredClientService.update(dto);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('sys:sysclient:delete')")
    public Result delete(@RequestBody List<String> ids) {
        AssertUtils.isListEmpty(ids, "数组参数不能为空");
        oauth2RegisteredClientService.delete(ids);
        return Result.ok();
    }
}